package helpers;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * This is a helper class that reads a text file that
 * stores certain configuration. The format is very
 * simple and is of the following:
 *
 * Parameter = Value
 *
 * The configuration file can be found in the root
 * directory and is called
 *
 *      configuration.txt
 *
 * @author psyomn
 */
public class Configuration {
    private static Configuration inst = null;
    private static String mFilename = new String("configuration.txt");
    private static HashMap<String, String> mAttributes = new HashMap<String, String>();

    /**
     *
     * @return
     */
    public synchronized static Configuration inst() {
        if (inst==null)
        {
            inst = new Configuration();
        }
        return inst;
    }

    /** TODO add exception which will be able to query user for location or search other dirs
     * Upon creation, read the file and store in memory */
    private Configuration()
    {
        try
        {
            BufferedReader buff = new BufferedReader(new FileReader(mFilename));
            String line;
            String[] tmp;
            line = buff.readLine();

            FileReader fr = new FileReader(mFilename); 
            
            
            while(line!=null)
            {
            		line = line.trim(); 
                
            		/* 
            		 * If the first char is a '#' it means it is 
            		 * a comment
            		 */
            		if (line.charAt(0) != '#')
            		{
	            		tmp = line.split("\\s+=\\s+");
	                mAttributes.put(tmp[0], tmp[1]);
            		}
            		
            		line = buff.readLine();
            }
        }

        catch (Exception e)
        {
            System.out.println("No such file exists");
            e.printStackTrace();
        }
        
        System.out.println("Configuration Singleton initialized with file : ");
        
    }

    /**
     *
     * @return
     */
    public static String debug()
    {
        return "" + mAttributes;
    }

    /**
     *
     * @param att
     * @return
     */
    public static String getAttribute(String att)
    {
        if (inst==null)
            inst();

        if (mAttributes.containsKey(att)){
            return mAttributes.get(att);
        } else {
            return null;
        }
    }
    
  
}
